//
//  MHTagsTableView.m
//  MiaoCircle
//
//  Created by 许正荣 on 2018/5/11.
//  Copyright © 2018年 acgera. All rights reserved.
//

#import "MHTagsTableView.h"
#import "MHTagTableViewCell.h"
#import "MHTagViewTableViewCell.h"
#import "MHCommonHeaderView.h"
#import "MHTagTableHeaderView.h"
#import "MHCircleUserTag.h"

@interface MHTagsTableView()


@end

@implementation MHTagsTableView




-(instancetype) initWithData:(NSArray *) data;
{
//    self = [[MHTagsTableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    self.tagsData = data;
    if (self) {
        self.dataSource = self;
        self.delegate = self;
        [self mh_registerNibCell:MHTagTableViewCell.class] ;
        [self mh_registerNibCell:MHTagViewTableViewCell.class] ;
        self.backgroundColor = MHBGColorLikeGray;
        [self setSeparatorColor:MHBGColorLikeGray];
        self.tableFooterView = [UIView new];
        
        
    }
    
    return self;
    
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (self.tagsData.count >= 2) {
        
        if (section == 0) {
            MHCommonHeaderView *headerView = [MHCommonHeaderView headerViewWithTableView:tableView];
            headerView.contentLabel.text = @"圈主标签";
            return headerView;
        }else
        {
           return [self configHeaderView];
        }
        
    }else
    {
        return [self configHeaderView];
    }
    
}


-(void)setTagsData:(NSArray *)tagsData
{
    _tagsData = tagsData;
    [self reloadData];
}

-(MHTagTableHeaderView *) configHeaderView
{
    MHTagTableHeaderView *headerView = [MHTagTableHeaderView mh_viewFromXib];
    __weak __typeof(self)weakSelf = self;
    [headerView.addTagBtn bk_whenTapped:^{
        if (weakSelf.addTagBlock) {
            weakSelf.addTagBlock();
        }
    }];
    return headerView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return  30;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    NSArray *sectionData =  self.tagsData[indexPath.section];
    if (self.tagsData.count >= 2) {
        
        if (indexPath.section == 0) {
            MHTagViewTableViewCell *cell =  [tableView dequeueReusableCellWithIdentifier:@"MHTagViewTableViewCell"];
            [cell.tagView removeAllTags];
            cell.tagView.preferredMaxLayoutWidth = [UIScreen mainScreen].bounds.size.width;
            cell.tagView.padding = UIEdgeInsetsMake(20, 20, 20, 20);
            cell.tagView.lineSpacing = 20;
            cell.tagView.interitemSpacing = 30;
            cell.tagView.singleLine = NO;
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            NSArray *firstObject = sectionData;
            [firstObject enumerateObjectsUsingBlock:^(MHCircleUserTag *obj, NSUInteger idx, BOOL * _Nonnull stop) {
                
                SKTag *tag = [[SKTag alloc] initWithText:obj.tagName];
                tag.font = [UIFont systemFontOfSize:FontSizeFromPixel(20)];
                tag.textColor = MHTextColor4ABC6A;
                tag.cornerRadius = 5;
                tag.enable = YES;
                tag.padding = UIEdgeInsetsMake(5, 10, 5, 10);
                [cell.tagView addTag:tag];
            }];
            
            cell.tagView.didTapTagAtIndex = ^(NSUInteger index)
            {
                if (self.tapCircleTagBlock) {
                    self.tapCircleTagBlock(sectionData[index]);
                }
                
            };
            return cell;
            
            
        }else
        {
            return [self dequeueCellWithTableView:tableView  Identify:@"MHTagTableViewCell" indexPath:indexPath];
        }
        
    }else
    {
        return [self dequeueCellWithTableView:tableView  Identify:@"MHTagTableViewCell" indexPath:indexPath];
    }
}

-(MHTagTableViewCell *) dequeueCellWithTableView:(UITableView *) tableView  Identify:(NSString *)identify indexPath:(NSIndexPath *) indexPath
{
    NSArray *sectionData =  self.tagsData[indexPath.section];
    MHTagTableViewCell *cell =  [tableView dequeueReusableCellWithIdentifier:identify];
    MHCircleUserTag *tag = sectionData[indexPath.row];
    cell.tagName.text =  tag.tagName;
    __weak __typeof(self)weakSelf = self;
    [cell.deleteBtn bk_whenTapped:^{
        MHCircleUserTag *tag = sectionData[indexPath.row];
        if (weakSelf.removeTagBlock) {
            weakSelf.removeTagBlock(tag.autoId );
        }
    }];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSArray *sectionData =  self.tagsData[indexPath.section];
    if (self.selectCellBlock) {
        MHCircleUserTag *tag = sectionData[indexPath.row];
        self.selectCellBlock(tag);
    }
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSArray *sectionData =  self.tagsData[section];
    if (section == 0 ) {
        if (sectionData.count) {
                return  1;
        }else
        {
                return  0;
        }
    
    }else
    {
         return sectionData.count;
    }
   
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.tagsData.count;
}


@end
